package com.boot.main.controller;


import com.boot.main.config.redis.JedisUtil;
import com.boot.main.response.ResponseDataUtil;
import com.boot.main.utils.Md5;
import com.boot.main.entity.CcomUser;
import com.boot.main.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/v1/api")
@Api(tags = "用户模块")
public class UserController {
    @Resource
    private IUserService userService;

    @RequestMapping(value="/users", method = RequestMethod.GET)
    @ApiOperation(value = "获取用户列表")
    public Object getUsers(@RequestParam("page") Integer page,
                           @RequestParam("limit") Integer limit,
                           @RequestParam("mobile") String mobile,
                           @RequestParam("username") String username){
        return userService.getUsers(page, limit, mobile, username);
    }

    @RequestMapping(value="/user", method = RequestMethod.POST)
    @ApiOperation(value = "保存用户")
    public Object saveUser(@RequestBody CcomUser user) {
        user.setCreate_time((new Date()).getTime());
        Md5 md5 = new Md5();
        try {
            user.setPassword(md5.GetMD5Code("888888"));
        }catch (Exception e){
            e.printStackTrace();
        }
        return userService.saveUser(user);
    }

    @RequestMapping(value="/blog/user", method = RequestMethod.POST)
    @ApiOperation(value = "保存用户")
    public Object saveFrontUser(@RequestBody CcomUser user, @RequestParam("code") String code) {
        user.setCreate_time((new Date()).getTime());
        user.setStatus(1);
        user.setRole_id("6");
        user.setName(user.getUsername());
        String captcha = JedisUtil.getJson("code");
        if(!captcha.equals(code)){
            return ResponseDataUtil.error("验证码不正确！", null);
        }
        return userService.saveFrontUser(user);
    }

    @RequestMapping(value="/user/{id}", method = RequestMethod.PUT)
    @ApiOperation(value="更新用户")
    public Object updateUser(@RequestBody CcomUser user){
        // user.setCreate_time((new Date()).getTime());
        return userService.updateUser(user);
    }

    @RequestMapping(value="/user/me/{id}", method = RequestMethod.PUT)
    @ApiOperation(value="更新当前用户信息")
    public Object updateUserMe(@RequestBody Map map, @PathVariable Long id){
        return userService.updateUserMe(map, id);
    }

    @RequestMapping(value="/user/{id}", method = RequestMethod.DELETE)
    @ApiOperation(value="删除用户")
    public Object deleteUser(@PathVariable Long id){
        return userService.deleteUser(id);
    }

    @RequestMapping(value="/user/password/reset/{id}", method = RequestMethod.PUT)
    @ApiOperation(value = "重置密码")
    public Object resetPassword(@PathVariable Long id){
        return userService.resetPassword(id);
    }

    @RequestMapping(value="/user/password", method = RequestMethod.PUT)
    @ApiOperation(value="修改密码")
    public Object updatePassword(@RequestBody Map map){
        Long id = Long.parseLong(map.get("id").toString());
        String password = map.get("password").toString();
        String newPassword = map.get("newPassword").toString();
        return userService.updatePassword(id, password, newPassword);
    }
}
